package blog.controller;

import blog.common.Result;
import blog.dto.VideoDetails;
import blog.entity.Video;
import blog.entity.VideoSrc;
import blog.entity.VideoType;
import blog.service.VideoService;
import blog.service.VideoSrcService;
import blog.service.VideoTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/video")
@Slf4j
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoSrcService videoSrcService;

    @Autowired
    private VideoTypeService videoTypeService;

    /**
     * 查看视频列表，支持分页，支持按照视频类型，视频名称，演员，导演进行查询
     * @return
     * http://localhost:8080/video?typeId=1&typeName=电影&name=张三&performer=沈腾，成龙&director=新海诚&page=1&pageSize=10
     */
    @GetMapping
    public Result getAllVideo(@RequestParam(value = "typeId", required = false) Integer typeId,
                                @RequestParam(value = "typeName", required = false) String typeName,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "performer", required = false) String performer,
                              @RequestParam(value = "director", required = false) String director,
                              @RequestParam("page") Integer page,
                              @RequestParam("pageSize") Integer pageSize){
        //1.判断参数是否合法
        if (page < 1 || pageSize < 1){
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        Page<Video> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        if (typeName != null){
            VideoType videoType = videoTypeService.getOne(new LambdaQueryWrapper<VideoType>().eq(VideoType::getTypeName, typeName));
            if (videoType != null){
                typeId = videoType.getId();
            }
        }
        if (typeId != null){
            wrapper.eq(Video::getTypeId, typeId);
        }

        if (name != null){
            wrapper.like(Video::getName, name);
        }
        if (performer != null){
            wrapper.like(Video::getPerformer, performer);
        }
        if (director != null){
            wrapper.like(Video::getDirector, director);
        }
        wrapper.orderByDesc(Video::getCreateTime);
        Page<Video> userPage1 = videoService.page(userPage, wrapper);
        //3.返回结果
        return Result.success(userPage1, "查询成功");



    }
    /**
     * 查看视频详情
     * @return
     * http://localhost:8080/video?id=2
     */
    @GetMapping("/{id}")
    public Result getVideoById(@PathVariable("id")  Integer id) {
        //1.判断参数是否合法
        if (id == null || id < 1) {
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        Video video = videoService.getById(id);
        if (video == null){
            return Result.error("视频不存在");
        }
        //定义返回对象
        VideoDetails videoDetails = new VideoDetails();
        videoDetails.setVideo(video);
        //查询视频的类型名称
        videoDetails.setTypeName(videoTypeService.getTypeNameById(video.getTypeId()));

        //查询视频的播放地址
        List<VideoSrc> src = videoSrcService.listSrcByVideoId(id);
        videoDetails.setVideoSrcList(src);

        //3.返回结果
        return Result.success(videoDetails, "查询成功");


    }

    /**
     * 分页获取视频的播放地址
     */
    @GetMapping("/getVideoSrc")
    public Result getVideoSrc(
                              @RequestParam("page") Integer page,
                              @RequestParam("pageSize") Integer pageSize){
        //1.判断参数是否合法
        if ( page < 1 || pageSize < 1){
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        Page<VideoSrc> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<VideoSrc> wrapper = new LambdaQueryWrapper<>();
        Page<VideoSrc> userPage1 = videoSrcService.page(userPage, wrapper);
        //3.返回结果
        return Result.success(userPage1, "查询成功");
    }



}